home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / test / pixmap_browser.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-07-27  |  3.9 KB  |  170 lines

  1. //
  2. // "$Id: pixmap_browser.cxx,v 1.5.2.1 1999/07/27 17:24:14 bill Exp $"
  3. //
  4. // Another pixmap test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // On purpose, I do NOT provide a fltk method to turn a file
  7. // into a pixmap.  This program uses a rather simplistic one.
  8. //
  9. // Copyright 1998-1999 by Bill Spitzak and others.
  10. //
  11. // This library is free software; you can redistribute it and/or
  12. // modify it under the terms of the GNU Library General Public
  13. // License as published by the Free Software Foundation; either
  14. // version 2 of the License, or (at your option) any later version.
  15. //
  16. // This library is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. // Library General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU Library General Public
  22. // License along with this library; if not, write to the Free Software
  23. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  24. // USA.
  25. //
  26. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  27. //
  28.  
  29. #include <FL/Fl.H>
  30. #include <FL/Fl_Box.H>
  31. #include <FL/Fl_Window.H>
  32. #include <FL/Fl_Button.H>
  33. #include <FL/Fl_Pixmap.H>
  34. #include <ctype.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <errno.h>
  38. #include <FL/fl_file_chooser.H>
  39. #include <FL/fl_message.H>
  40.  
  41. Fl_Box *b;
  42. Fl_Window *w;
  43.  
  44. char **data;
  45. int sizeofdata;
  46. int numlines;
  47.  
  48. static int hexdigit(int x) {
  49.   if (isdigit(x)) return x-'0';
  50.   if (isupper(x)) return x-'A'+10;
  51.   if (islower(x)) return x-'a'+10;
  52.   return 20;
  53. }
  54.  
  55. int load_file(const char *name) {
  56.   FILE *f = fopen(name,"r");
  57.   if (!f) {
  58.     fl_message("Can't open %s, %s",name,strerror(errno));
  59.     return 0;
  60.   }
  61.   if (data) {
  62.     for (int i=numlines; i--;) delete[] data[i];
  63.   }
  64. #define BUFSIZE 2048
  65.   char buffer[BUFSIZE];
  66.   int i = 0;
  67.   while (fgets(buffer, BUFSIZE, f)) {
  68.     if (buffer[0] != '\"') continue;
  69.     char *p = buffer;
  70.     char *q = buffer+1;
  71.     while (*q != '\"') {
  72.       if (*q == '\\') switch (*++q) {
  73.       case '\n':
  74.     fgets(q,(buffer+BUFSIZE)-q,f); break;
  75.       case 0:
  76.     break;
  77.       case 'x': {
  78.     q++;
  79.     int n = 0;
  80.     for (int x = 0; x < 3; x++) {
  81.       int d = hexdigit(*q);
  82.       if (d > 15) break;
  83.       n = (n<<4)+d;
  84.       q++;
  85.     }
  86.     *p++ = n;
  87.       } break;
  88.       default: {
  89.     int c = *q++;
  90.     if (c>='0' && c<='7') {
  91.       c -= '0';
  92.       for (int x=0; x<2; x++) {
  93.         int d = hexdigit(*q);
  94.         if (d>7) break;
  95.         c = (c<<3)+d;
  96.         q++;
  97.       }
  98.     }
  99.     *p++ = c;
  100.       } break;
  101.       } else {
  102.     *p++ = *q++;
  103.       }
  104.     }
  105.     *p++ = 0;
  106.     if (i >= sizeofdata) {
  107.       sizeofdata = 2*sizeofdata+100;
  108.       char **newdata = new char *[sizeofdata];
  109.       for (int j=0; j<i; j++) newdata[j] = data[j];
  110.       delete[] data;
  111.       data = newdata;
  112.     }
  113.     data[i] = new char[p-buffer];
  114.     memcpy(data[i],buffer,p-buffer);
  115.     i++;
  116.   }
  117.   numlines = i;
  118.   fclose(f);
  119.   return i;
  120. }
  121.  
  122. Fl_Pixmap *pixmap;
  123. void newpixmap() {
  124.   delete pixmap;
  125.   pixmap = new Fl_Pixmap(data);
  126.   pixmap->label(b);
  127.   w->redraw();
  128. }
  129.  
  130. static char name[1024];
  131.  
  132. void file_cb(const char *n) {
  133.   if (!strcmp(name,n)) return;
  134.   if (!load_file(n)) return;
  135.   strcpy(name,n);
  136.   w->label(name);
  137.   newpixmap();
  138. }
  139.  
  140. void button_cb(Fl_Widget *,void *) {
  141.   fl_file_chooser_callback(file_cb);
  142.   fl_file_chooser("XPM file","*.xpm",name);
  143.   fl_file_chooser_callback(0);
  144. }
  145.  
  146. int dvisual = 0;
  147. int arg(int, char **argv, int &i) {
  148.   if (argv[i][1] == '8') {dvisual = 1; i++; return 1;}
  149.   return 0;
  150. }
  151.  
  152. int main(int argc, char **argv) {
  153.   int i = 1;
  154.   if (Fl::args(argc,argv,i,arg) < argc)
  155.     Fl::fatal(" -8 # : use default visual\n%s\n",Fl::help);
  156.  
  157.   Fl_Window window(400,400); ::w = &window;
  158.   Fl_Box b(0,0,window.w(),window.h()); ::b = &b;
  159.   Fl_Button button(5,5,100,35,"load");
  160.   button.callback(button_cb);
  161.   if (!dvisual) Fl::visual(FL_RGB);
  162.   window.resizable(window);
  163.   window.show(argc,argv);
  164.   return Fl::run();
  165. }
  166.  
  167. //
  168. // End of "$Id: pixmap_browser.cxx,v 1.5.2.1 1999/07/27 17:24:14 bill Exp $".
  169. //
  170.